<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Material of Analysis | 自定义主题</title>
    <meta name="description" content="The analytical data that you often use to learn">
    <link rel="icon" href="/material/logo.png">
  <link rel="manifest" href="/material/manifest.json">
  <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/material/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="/material/icons/safari-pinned-tab.svg" color="#3eaf7c">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
  <meta name="msapplication-TileColor" content="#000000">
    
    <link rel="preload" href="/material/assets/css/0.styles.89749010.css" as="style"><link rel="preload" href="/material/assets/js/app.671f232e.js" as="script"><link rel="preload" href="/material/assets/js/6.36baf8b1.js" as="script"><link rel="prefetch" href="/material/assets/js/9.6ad28882.js"><link rel="prefetch" href="/material/assets/js/1.a893eea4.js"><link rel="prefetch" href="/material/assets/js/2.ad32dd32.js"><link rel="prefetch" href="/material/assets/js/3.2af42d2c.js"><link rel="prefetch" href="/material/assets/js/4.b8a50edf.js"><link rel="prefetch" href="/material/assets/js/5.3a34f633.js"><link rel="prefetch" href="/material/assets/js/7.23741c7b.js"><link rel="prefetch" href="/material/assets/js/8.0b506f6a.js"><link rel="prefetch" href="/material/assets/js/10.aa5bc1f8.js"><link rel="prefetch" href="/material/assets/js/11.b5d01b0b.js"><link rel="prefetch" href="/material/assets/js/12.2aab57fe.js"><link rel="prefetch" href="/material/assets/js/13.17c8fcb3.js"><link rel="prefetch" href="/material/assets/js/14.b0d8de38.js"><link rel="prefetch" href="/material/assets/js/15.95c4b4d8.js"><link rel="prefetch" href="/material/assets/js/16.8279b098.js"><link rel="prefetch" href="/material/assets/js/17.c88c3a9c.js"><link rel="prefetch" href="/material/assets/js/18.0635186f.js"><link rel="prefetch" href="/material/assets/js/19.5e722a99.js">
    <link rel="stylesheet" href="/material/assets/css/0.styles.89749010.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div><a href="/material/" class="home-link router-link-active"><!----><span class="site-name">
      Material of Analysis
    </span></a><div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""><!----></div><nav class="nav-links can-hide"><div class="nav-item"><a href="/material/basic/" class="nav-link">基础</a></div><a href="https://github.com/docschina/vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header><div class="sidebar-mask"></div><div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/material/basic/" class="nav-link">基础</a></div><a href="https://github.com/docschina/vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav><!----></div><div class="page"><div class="content"><h1 id="自定义主题"><a href="#自定义主题" aria-hidden="true" class="header-anchor">#</a> 自定义主题</h1><div class="tip custom-block"><p class="custom-block-title">提示</p><p>主题组件同样受到<a href="/material/guide/using-vue.html#browser-api-access-restrictions">浏览器 API 访问限制</a>。</p></div><p>VuePress 使用 Vue 单个文件组件来定制主题。要使用自定义布局，请在文档根目录下创建一个 <code>.vuepress/theme</code> 目录，然后创建一个 <code>Layout.vue</code> 文件：</p><pre class="language-text"><code>.
└─ .vuepress
   └─ theme
      └─ Layout.vue
</code></pre><p>这儿和开发一个正常的 Vue 应用程序是一样的。完全取决于你如何组织你的主题。</p><h2 id="网站和网页元数据-site-and-page-metadata"><a href="#网站和网页元数据-site-and-page-metadata" aria-hidden="true" class="header-anchor">#</a> 网站和网页元数据(site and page metadata)</h2><p>对于 <code>docs</code> 中的每个 <code>.md</code> 文件，<code>Layout</code>组件将只被调用一次，并且整个站点和该特定页面的元数据将分别暴露为 <code>this.$site</code> 和 <code>this.$page</code> 属性被注入到应用程序中的每个组件里。</p><p>这是这个网站的 <code>$site</code> 值：</p><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;title&quot;</span><span class="token operator">:</span> <span class="token string">&quot;VuePress&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;description&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Vue-powered Static Site Generator&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;base&quot;</span><span class="token operator">:</span> <span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;pages&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
      <span class="token property">&quot;path&quot;</span><span class="token operator">:</span> <span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span>
      <span class="token property">&quot;title&quot;</span><span class="token operator">:</span> <span class="token string">&quot;VuePress&quot;</span><span class="token punctuation">,</span>
      <span class="token property">&quot;frontmatter&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    ...
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre><p><code>title</code>，<code>description</code> 和 <code>base</code> 从 <code>.vuepress/config.js</code> 中的相应字段复制而来。 <code>pages</code> 包含每个页面的元数据对象数组，包括其路径，页面标题（在 <a href="/material/guide/markdown.html#yaml-front-matter">YAML front matter</a> 中显式指定或从页面上的第一个标题推断）以及该文件中的任意 YAML frontmatter 数据。</p><p>这是你正在查看的此页面的 <code>$page</code> 对象：</p><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;path&quot;</span><span class="token operator">:</span> <span class="token string">&quot;/custom-themes.html&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;title&quot;</span><span class="token operator">:</span> <span class="token string">&quot;自定义主题&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;headers&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>/* ... */<span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token property">&quot;frontmatter&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><p>如果用户在 <code>.vuepress/config.js</code> 中提供 <code>themeConfig</code>，它也可以用 <code>$site.themeConfig</code>。你可以使用它来允许用户自定义主题的行为 - 例如，指定类别和页面顺序。然后，你可以将这些数据与 <code>$site.pages</code> 一起使用来动态构建导航链接。</p><p>最后，别忘了 <code>this.$route</code> 和 <code>this.$router</code> 也可作为 Vue Router API 的一部分。</p><h2 id="内容摘要-content-excerpt"><a href="#内容摘要-content-excerpt" aria-hidden="true" class="header-anchor">#</a> 内容摘要(content excerpt)</h2><p>如果 markdown 文件包含一个 <code>&lt;!-- more --&gt;</code> 注释，那么注释上方的任何内容都将被提取，然后暴露在 <code>$page.excerpt</code> 中。如果你正在为博客创建自定义主题，可以使用此数据来渲染带有摘要信息的文章列表。</p><h2 id="内容出口-content-outlet"><a href="#内容出口-content-outlet" aria-hidden="true" class="header-anchor">#</a> 内容出口(content outlet)</h2><p>所渲染的当前 <code>.md</code> 文件的编译内容将作为特殊的 <code>&lt;Content/&gt;</code> 全局组件来使用。你需要将其呈现在布局的某个位置以显示页面的内容。最简单的主题可以是一个带有以下内容的 <code>Layout.vue</code> 组件：</p><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>theme-container<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Content</span><span class="token punctuation">/&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
</code></pre><h2 id="应用级别的增强-app-level-enhancements"><a href="#应用级别的增强-app-level-enhancements" aria-hidden="true" class="header-anchor">#</a> 应用级别的增强(app level enhancements)</h2><p>主题可以通过在主题根文件的 <code>enhanceApp.js</code> 文件来增强 VuePress 使用的 Vue 应用。该文件应该通过 <code>export default</code> 命令导出一个钩子函数，并接受一个包含了一些应用级别属性的对象作为参数。你可以使用这个钩子来安装一些附加的 Vue 插件、注册全局组件，或者增加额外的路由钩子：</p><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">(</span><span class="token punctuation">{</span>
  Vue<span class="token punctuation">,</span> <span class="token comment">// VuePress 应用程序正在使用的 Vue 构造函数</span>
  options<span class="token punctuation">,</span> <span class="token comment">// 用于 Vue 根实例的一些选项</span>
  router<span class="token punctuation">,</span> <span class="token comment">// 用于应用程序的 router 实例</span>
  siteData <span class="token comment">// 网站元数据</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// ……执行一些其他的应用程序级别的优化</span>
<span class="token punctuation">}</span>
</code></pre><h2 id="从依赖中引用主题-using-theme-from-a-dependency"><a href="#从依赖中引用主题-using-theme-from-a-dependency" aria-hidden="true" class="header-anchor">#</a> 从依赖中引用主题(using theme from a dependency)</h2><p>主题可以在 npm 上以原始 Vue SFC 格式发布为 <code>vuepress-theme-xxx</code>。</p><p>要使用 npm 依赖项的主题，请在 <code>.vuepress/config.js</code> 中提供一个 <code>theme</code> 选项：</p><pre class="language-js"><code>module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  theme<span class="token punctuation">:</span> <span class="token string">'awesome'</span>
<span class="token punctuation">}</span>
</code></pre><p>VuePress 将尝试定位并使用 <code>node_modules/vuepress-theme-awesome/Layout.vue</code>。</p><h2 id="自定义默认主题"><a href="#自定义默认主题" aria-hidden="true" class="header-anchor">#</a> 自定义默认主题</h2><p><code>vuepress eject [targetDir]</code> 命令会将默认主题的源代码复制到 <code>.vuepress/theme</code> 中，来允许彻底的自定义。但是请注意，一旦你 eject，即使你升级 VuePress 版本，你这仍然是自己的主题，并且不会收到对默认主题的未来更新或错误修复。</p><hr><blockquote><p>原文：<a href="https://vuepress.vuejs.org/guide/custom-themes.html" target="_blank" rel="noopener noreferrer">https://vuepress.vuejs.org/guide/custom-themes.html</a></p></blockquote><hr></div><div class="content edit-link"><a href="https://github.com/docschina/vuepress/edit/master/docs/guide/custom-themes.md" target="_blank" rel="noopener noreferrer">在GitHub上编辑此页</a><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div><!----></div></div></div>
    <script src="/material/assets/js/6.36baf8b1.js" defer></script><script src="/material/assets/js/app.671f232e.js" defer></script>
  </body>
</html>
